﻿using CGT.API.API;
using CGT.API.Common;
using CGT.API.Models;
using OF.Component.BLL;
using OF.Component.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CGT.API.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    public class UserController : BaseApiController
    {
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<UserIDCardModel> GetIDCard(long userId)
        {
            var oInfo = IDCardOperate.GetByUserID(userId);
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResIDCardGet result = usrClient.IDCard_GetByUserAsync(userId).Result;
         
            return ResultData<UserIDCardModel>.Success(new UserIDCardModel()
            {
                UserID = userId,
                IDNumComplete = oInfo.IDNum,
                IDNum = oInfo.IDNum.Substring(0, 6) + "********" + oInfo.IDNum.Substring(14),
                UserName = oInfo.Name
            });
        }
        /// <summary>
        /// 根据用户id修改用户手机号
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newMobile"></param>
        /// <param name="verifycode"></param>
        /// <returns></returns>
        public ResultData<string> UpdateMobile(long userId, string newMobile, string verifycode)
        {
            SMSClient smsClient = new SMSClient();
            ResultDataOfBoolean valid = smsClient.CheckVerificationCodeAsync(newMobile, verifycode, 100).Result;
            if (!valid.Data.Value)
            {
                return ResultData<string>.Error("验证码错误,请重新输入");
            }
            var usrInfo = UserOperate.GetByID(userId);
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseUserGet userInfoResult = usrClient.User_GetAsync(userId).Result;
            if (usrInfo != null)
            {
                //ResponseUserGet userInfo = userInfoResult.Data;

                //var model = new RequestUserUpdate();
                //model.UserID = userId;
                //model.UserMobile = newMobile;
                //model.CityID = userInfo.CityID;
                //model.DistributorID = userInfo.DistributorID;
                //model.HeadImg = userInfo.HeadImg;
                //model.Sex = userInfo.Sex;
                //model.UserLevel = userInfo.UserLevel;
                //model.UserName = userInfo.UserName;
                //model.UserPassword = userInfo.UserPassword;
                //model.UserSource = userInfo.UserSource;
                //model.UserType = userInfo.UserType;
                //ResultDataOfResponseUserUpdate result = usrClient.User_UpdateAsync(model).Result;
                usrInfo.UserMobile = newMobile;
                usrInfo.UpdatedDate = DateTime.Now;
                UserOperate.Update(usrInfo);
                return ResultData<string>.Success("更新成功");
            }
            else
            {
                return ResultData<string>.Error("更新失败，用户不存在");
            }
        }
    }
}
